<?php

declare(strict_types=1);

namespace Plugin\Attractions\HaiKang\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Plugin\Attractions\HaiKang\Model\HkDevice;

class DeviceRepsitory extends IRepository
{
    public function __construct(protected readonly HkDevice $model) {}

    public function handleSearch(Builder $query, array $params): Builder
    {
        return $query
            ->when(Arr::get($params, 'deviceID'), static function (Builder $query, $device_id) {
                $query->whereIn('device_id', Arr::wrap($device_id));
            })
            ->when(Arr::get($params, 'deviceModel'), static function (Builder $query, $device_model) {
                $query->whereIn('device_model', Arr::wrap($device_model));
            })
            ->when(Arr::get($params, 'deviceName'), static function (Builder $query, $device_name) {
                $query->where('device_name', 'like', '%' . $device_name . '%');
            })
            ->when(Arr::get($params, 'deviceSerial'), static function (Builder $query, $device_serial) {
                $query->whereIn('device_serial', Arr::wrap($device_serial));
            })
            ->when(Arr::get($params, 'deviceStatus'), static function (Builder $query, $device_status) {
                $query->where('device_status', $device_status);
            })
            ->when(Arr::get($params, 'groupID'), static function (Builder $query, $group_id) {
                $query->whereIn('group_id', Arr::wrap($group_id));
            })
            ->when(Arr::get($params, 'isEncrypt'), static function (Builder $query, $isEncrypt) {
                $query->where('is_encrypt', $isEncrypt);
            })
            ->when(Arr::get($params, 'deviceVersion'), static function (Builder $query, $deviceVersion) {
                $query->where('device_version', $deviceVersion);
            })
            ->when(Arr::get($params, 'defence'), static function (Builder $query, $defence) {
                $query->where('defence', $defence);
            });
    }

    public function list(array $params = []): Collection
    {
        return $this->perQuery($this->getQuery(), $params)
            ->get([
                'device_id',
                'device_model',
                'device_name',
                'device_serial',
                'device_status',
                'group_id',
                'is_encrypt',
                'device_version',
                'defence'
            ]);
    }

    public function listDeviceAndChannel(array $params = []): Collection
    {
        return $this->perQuery($this->getQuery(), $params)
            ->with('channel')
            ->get([
                'device_id',
                'device_model',
                'device_name',
                'device_serial',
                'device_status',
                'group_id',
                'is_encrypt',
                'device_version',
                'defence',
                'channel.channel_id',
                'channel.channel_name',
                'channel.channel_no',
                'channel.channel_status',
                'channel.channel_type',
                'channel.is_use',
                'channel.ipc_serial',
                'channel.child_device_model'
            ]);
    }
}
